/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package at.dcmspl;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

/**
 * The Bean Managed Transaction ProcessingNodeBMT
 * @author Hannes Stornig, Gosch Daniel
 */

@TransactionManagement(TransactionManagementType.BEAN)
@Stateful
public class ProcessingNodeBMT implements ProcessingNodeBMTRemote{
    
    @Resource SessionContext sc;
       
    @Override
    public void doSomething() throws IOException, NamingException{
        Logger.getLogger(ProcessingNodeBMT.class.getName()).log(Level.INFO, "###ProcessingNode Main");
        Logger.getLogger(ProcessingNodeBMT.class.getName()).log(Level.INFO, "### STARTING CLIENT........");
        //JNDI LOOKUP
        Context context = new InitialContext();
        DcmQueueProducerServiceRemote dcmQueueProducerService = (DcmQueueProducerServiceRemote) context.lookup("java:global/DcmQueueService-ejb/DcmQueueService!at.dcmspl.DcmQueueProducerServiceRemote");
        DcmQueueConsumerServiceRemote dcmQueueConsumerService = (DcmQueueConsumerServiceRemote) context.lookup("java:global/DcmQueueService-ejb/DcmQueueService!at.dcmspl.DcmQueueConsumerServiceRemote"); 
        
        dcmQueueProducerService.createDcmQueue("1.Queue");
        dcmQueueProducerService.addDcmFile("1.Queue", "eins");
        dcmQueueProducerService.addDcmFile("1.Queue", "zwei");

        dcmQueueProducerService.addDcmFile("1.Queue", "drei");
        //utx.commit();
        //utx = sc.getUserTransaction();
        //utx.begin();
        UserTransaction utx = sc.getUserTransaction();
        try{
            
            utx.begin();
            DcmFile pool = dcmQueueConsumerService.poolDcmFile(dcmQueueConsumerService.findDcmQueueByName("1.Queue"));
            dcmQueueProducerService.addDcmFile("1.Queue", "vier");
            dcmQueueProducerService.addDcmFile("1.Queue", "funf");
            //utx.rollback();
            dcmQueueProducerService.createDcmQueue("2.Queue");
            dcmQueueProducerService.createDcmQueue("3.Queue");
            
            dcmQueueProducerService.addDcmFile(dcmQueueProducerService.findDcmQueueByName("2.Queue"), pool);
            dcmQueueProducerService.addDcmFile(dcmQueueProducerService.findDcmQueueByName("3.Queue"), pool);
            dcmQueueConsumerService.poolDcmFile(dcmQueueProducerService.findDcmQueueByName("2.Queue"));
            dcmQueueConsumerService.poolDcmFile(dcmQueueProducerService.findDcmQueueByName("3.Queue"));
            utx.commit();
            
            
            //utx.rollback();
        }catch(Throwable t){
            try{
                utx.rollback();
            }catch(Exception ex){
                System.out.println("Transaktion fehlgeschlagen: " + t.getMessage());
                Logger.getLogger(ProcessingNodeBMT.class.getName()).log(Level.INFO, "Error: {0}",ex);
            }
        }
        
    }

    
}
